package com.zzyl.nursing.service.impl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.framework.config.TimeSlotUtils;
import com.zzyl.nursing.domain.CountByTime;
import com.zzyl.nursing.domain.PageResult;
import com.zzyl.nursing.dto.ReservationPageDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.ReservationMapper;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.service.IReservationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * 预约信息Service业务层处理
 *
 * @author zs
 * @date 2025-10-14
 */
@Service
public class ReservationServiceImpl extends ServiceImpl<ReservationMapper, Reservation> implements IReservationService {
    @Autowired
    private ReservationMapper reservationMapper;

    /**
     * 查询预约信息
     *
     * @param id 预约信息主键
     * @return 预约信息
     */
    @Override
    public Reservation selectReservationById(Long id) {
        return reservationMapper.selectById(id);
    }

    /**
     * 查询预约信息列表
     *
     * @param reservation 预约信息
     * @return 预约信息
     */
    @Override
    public List<Reservation> selectReservationList(Reservation reservation) {
        return reservationMapper.selectReservationList(reservation);
    }

    /**
     * 新增预约信息
     *
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int insertReservation(Reservation reservation) {
        reservation.setStatus(0);
        return reservationMapper.insert(reservation);
    }

    /**
     * 修改预约信息
     *
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int updateReservation(Reservation reservation) {
        return reservationMapper.updateById(reservation);
    }

    /**
     * 批量删除预约信息
     *
     * @param ids 需要删除的预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationByIds(Long[] ids) {
        return reservationMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除预约信息信息
     *
     * @param id 预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationById(Long id) {
        return reservationMapper.deleteById(id);
    }

    /**
     * 查询取消预约数量
     *
     * @param userId
     * @return
     */
    @Override
    public Integer getCancelledCount(Long userId) {
        long time = System.currentTimeMillis();
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);
        LambdaQueryWrapper<Reservation> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Reservation::getStatus, 2)
                .gt(Reservation::getUpdateTime, startTime)
                .lt(Reservation::getUpdateTime, endTime)
                .eq(Reservation::getId, userId);
        return Math.toIntExact(reservationMapper.selectCount(lqw));
    }

    @Override
    public PageResult<Reservation> selectReservationPage(ReservationPageDto reservationPageDto) {
        //开启分页
        PageHelper.startPage(reservationPageDto.getPageNum(), reservationPageDto.getPageSize());

        List<Reservation> list = reservationMapper.selectReservationPage(reservationPageDto);
        Page<Reservation> page = (Page<Reservation>) list;
        return new PageResult<>(page.getTotal(), page.getResult());
    }

    /**
     * 取消预约
     * @param id
     */
    @Override
    public void cancelReservation(Long id) {

        LambdaUpdateWrapper<Reservation> luw = new LambdaUpdateWrapper<>();
        luw.eq(Reservation::getId, id)
                .set(Reservation::getStatus, 2);

        reservationMapper.update(null, luw);
    }

    /**
     * 查询每个时间段剩余预约次数
     * 同一时间段只能有六位家属预约
     */
    @Override
    public List<CountByTime> countByTime(Long time) {
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);
        //创建一个装着时间段的集合list,根据每个时间段查询status=0的预约信息，计算数量
        List<String> strings = TimeSlotUtils.generateWorkTimeSlots(startTime, endTime);
        System.out.println(strings);

        return reservationMapper.countByTime(strings);
    }


    /**
     * 定时更新预约信息状态
     */
    @Transactional
    @Override
    public void updateReservationStatusJob() {
        //查询状态0的并且time小于当前时间的预约信息
        LambdaQueryWrapper<Reservation> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Reservation::getStatus, 0)
                .lt(Reservation::getTime, LocalDateTime.now());
        List<Reservation> reservations = reservationMapper.selectList(lqw);

        for (Reservation r : reservations) {
            r.setStatus(3);
        }
        //更新预约信息
        updateBatchById(reservations);
    }
}
